package com.sprucetec.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created with IntelliJ IDEA.
 * User: zhangyb
 * Date: 2018/10/15
 * Time: 11:31
 * Description:
 **/
@Configuration
@EnableAsync
public class SpringThreadPoolExecutorConfig {

    @Bean(name = "supportThreadPool")
    public ThreadPoolTaskExecutor supportThreadPool() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setKeepAliveSeconds(300);
        //核心线程池数
        pool.setCorePoolSize(5);
        //最大线程
        pool.setMaxPoolSize(30);
        //队列容量
        pool.setQueueCapacity(1000);
        //队列满，线程被拒绝执行策略
        pool.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
        pool.setThreadNamePrefix("live-service-");
        return pool;
    }

    @Bean(name = "threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setKeepAliveSeconds(300);
        //核心线程池数
        pool.setCorePoolSize(10);
        //最大线程
        pool.setMaxPoolSize(40);
        //队列容量
        pool.setQueueCapacity(1000);
        //队列满，线程被拒绝执行策略
        pool.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
        pool.setThreadNamePrefix("live-aop-");
        return pool;
    }



    /**
     * 聊天线程池
     * @return
     */
    @Bean(name = "liveChatThreadPool")
    public ThreadPoolTaskExecutor liveChatThreadPool() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setKeepAliveSeconds(300);
        //核心线程池数
        pool.setCorePoolSize(10);
        //最大线程
        pool.setMaxPoolSize(50);
        //队列容量
        //pool.setQueueCapacity(1000);
        //队列满，线程被拒绝执行策略
        pool.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
        pool.setThreadNamePrefix("live-chat-");
        return pool;
    }


    /*聊天消费线程池 针对于 Scheduled  模式 */
    @Bean("chatCustomerThreadPool")
    ThreadPoolTaskExecutor chatCustomerThreadPool(){
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(5);
        threadPoolTaskExecutor.setMaxPoolSize(20);
        threadPoolTaskExecutor.setQueueCapacity(200);
        threadPoolTaskExecutor.setThreadNamePrefix("live-chat-customer-");
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }


}
